package com.c8a.authservice.common;


import org.springframework.security.core.userdetails.User;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;// token附加信息类

import java.util.HashMap;
import java.util.Map;

public class CustomTokenEnhancer implements TokenEnhancer {

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
        // 获取登录信息
        User user = (User) oAuth2Authentication.getUserAuthentication().getPrincipal();
        Map<String, Object> customInfoMap = new HashMap<>();
        customInfoMap.put("loginName", user.getUsername());//登录名
        customInfoMap.put("name", user.getUsername());//用户姓名
        customInfoMap.put("content", "这是一个测试的内容");
        customInfoMap.put("authorities", "hahahaha");
        ((DefaultOAuth2AccessToken) oAuth2AccessToken).setAdditionalInformation(customInfoMap);
        return oAuth2AccessToken;
    }
}
